<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * Class UserMarketing
 *
 * @package App\Model
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing query()
 * @property int $id
 * @property int $user_id 用户user_id
 * @property string $title 标题
 * @property string $contents 文字
 * @property int $pop 人气
 * @property int $is_deleted 是否删除
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereContents($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereIsDeleted($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing wherePop($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereUserId($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Model\UserInterPushRecordMaterial[] $interPushRecordMaterial
 * @property-read \App\Model\UserInterPushInfo $interPushInfo
 * @property string|null $cover 封面
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereCover($value)
 * @property int $share 分享量
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserMarketing whereShare($value)
 */
class UserMarketing extends Model
{
    protected $table = "user_marketing";

    protected $fillable = ["title", "cover", "contents", "button_name", "link", "name", "phone"];


    public function add(Request $request)
    {
        $data = $request->input();
        $this->title = (string)$data['title'];
        $this->user_id = $request->user()->id;
        $this->contents = (string)$data['contents'];
        $this->cover = $data['cover'];
        $this->save();
        return $this->id;
    }


    /**
     * 获取个人网页互推匹配记录
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function interPushRecordMaterial()
    {
        return $this->morphByMany(UserInterPushRecordMaterial::class, "interPushRecordMaterials","source_type","source_id","id");
    }

    /**
     * 文章对应素材
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
     */
    public function interPushInfo()
    {
        return $this->morphOne(UserInterPushInfo::class,"material","material_type","material_id","id");
    }

}
